package com.geeaks.learn.multithread;

import java.time.LocalDateTime;

public class ObjectLockTest {
	
	public static void main(String[] args) {
		SynchronizedTest a = new SynchronizedTest();
		SynchronizedTest b = new SynchronizedTest();
		Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				a.method1();
			}
		},"thread-1");
		Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run() {
				b.method2();
			}
		},"thread-2");
		thread1.start();
		thread2.start();
		while(true) {}
	}
}

class SynchronizedTest {
	
	public synchronized void method1() {
		try {
			Thread.sleep(1000);
			System.out.println(Thread.currentThread().getName()+" 执行method1=== "+LocalDateTime.now());
		} catch (InterruptedException e) {
		}
	}
	
	public synchronized void method2() {
		try {
			Thread.sleep(1000);
			System.out.println(Thread.currentThread().getName()+" 执行method2=== "+LocalDateTime.now());
		} catch (InterruptedException e) {
		}
	}
	
	public static synchronized void method3() {
		try {
			Thread.sleep(1000);
			System.out.println(Thread.currentThread().getName()+" 执行method3=== "+LocalDateTime.now());
		} catch (InterruptedException e) {
		}
	}
	
	public static synchronized void method4() {
		try {
			Thread.sleep(1000);
			System.out.println(Thread.currentThread().getName()+" 执行method4=== "+LocalDateTime.now());
		} catch (InterruptedException e) {
		}
	}
}
